package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.service.ActiveService;
import com.cqut.forum.service.PictureReplyService;
import com.cqut.forum.service.PictureService;
import com.cqut.forum.service.UserService;

/**
 * 用户信息action
 * @author yangxun
 */
@Controller
public class UserAction{
	
	@Resource
	private UserService userService;
	@Resource
	private PictureService pictureService;
	@Resource
	private PictureReplyService pictureReplyService;
	@Resource
	private ActiveService articleService;
	@Resource
	private MessageAction messageAction;
	@Resource
	private UserNewsAction userNewsAction;
	
	@RequestMapping("/user/user_main")
	@ResponseBody
	public Map<String,Object> userMain(HttpServletRequest request,HttpServletResponse response,
			Map<String, Object> map){
		//属性说明：
		//userInfo为用户基本信息
		//userActive为用户动态
		//userFriends为用户好友
		//visitHistory为访问记录
		//friendNews为好友状态
		//各属性均为装有指定json格式的数组
		User user = (User) request.getSession().getAttribute("user");
		
		Map<String,Object> userInfo = new HashMap<String, Object>();
		//用户基本信息
		userInfo.put("userBase", userService.getUserBasicInfo(user));
		//用户提示信息
		userInfo.put("userActive", userService.getUserNotice(user));
		//用户好友
		userInfo.put("userFriends", userService.getFriends(user));
		//用户访问记录
		//userInfo.put("visitHistory", userService.getVisit(user));
		userInfo.put("visitHistory", userNewsAction.getModuleVisitors(user.getUserId(), "动态", 0, 9));
		//访问别人
		//userInfo.put("visitOther", userNewsAction.getVisitOther(user.getUserId(), "动态", 0, 9));
		//动态详细信息
		userInfo.put("activeInfo", userService.getActiveInfo(user,false,0,20));

		userInfo.put("ss", request.getSession().getServletContext().getAttribute("ss"));
		
		return userInfo;
	}
	
	@RequestMapping("/user/load_my_notice")
	@ResponseBody
	public List<Map<String, Object>> getNotice(HttpServletRequest request,
			int index,int size,String moduleName){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		if(moduleName.equals("dongtai")){
			result = userService.getActiveInfo(user,false,index,size);
		}
		else if(moduleName.equals("care")){
			result = userService.getActiveInfo(user,true,index,size);
		}
		else if(moduleName.equals("message")){
			//result = userService.getActiveInfo(user,true,index,size);
			result = messageAction.getMessages(request, index, size);
			//articleService.haveReadMyArticle(user.getUserId(),"留言");
			
		}
		else if(moduleName.equals("article")){
			articleService.haveReadMyArticle(user.getUserId(),"博客");
			
		}
		else if(moduleName.equals("discuss")){
			articleService.haveReadMyArticle(user.getUserId(),"讨论区");
			
		}
		else if(moduleName.equals("friendReply")){
			System.out.println("====================好友申请==================");
			
		}
		else if(moduleName.equals("picture")){
			result = pictureService.getMyUnread(user,index,size);
			pictureReplyService.haveReadMyPicture(user.getUserId());
		}
		return result;
	}
	
	/**
	 * 获取积分最多的用户，该规则按照userScore排名
	 * @param index
	 * @param size
	 * @return
	 */
	@RequestMapping("/user/hot_info")
	@ResponseBody
	public Map<String, Object> getMostScore(){
		int index =0;
		int size = 5; 
		//热门照片
		List<Map<String, Object>> hotPicture = pictureService.getHotPicture(index, size);
		//热门博主
		List<Map<String, Object>> hotUser = userService.getMostScore(index,size);
		//热门博客
		List<Map<String, Object>> hotArticle = articleService.getHotArticle(index, size);
		
		//封装返回客户端
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("hotPicture", hotPicture);
		result.put("hotUser", hotUser);
		result.put("hotArticle", hotArticle);
		return result;
	}
		
	/***************get set method****************/
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
